<?php
/**
 * Vote abstraction.
 *
 * @category Zend
 * @package Vote
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 * @license GPL v3
 */

class Application_Model_Vote extends Disputeo_Model_Abstract
{   
    /**
     * Setup model properties
     * 
     * @param type $options 
     */
    public function __construct($options = array())
    {
        $this->setTable(new Application_Model_DbTable_Vote());
        $this->setAttributes($options);
    }
    
    /**
     * Create a new vote
     * 
     * @param array $data
     * @return self 
     */
    public static function create(array $data)
    {
        $vote = new self();
        $vote->_add($data);
        
        return $vote;
    }
    
    /**
     * Get a dispute
     * 
     * @param integer $id
     * @return self 
     */
    public static function get($id)
    {
        $vote = new self();
        $vote->_find($id);
        
        return $vote;
    }
    
    /**
     * Add a vote
     * 
     * @param array $data
     * @return Application_Model_Vote
     */
    protected function _add(array $data)
    {
        $sql = 'INSERT INTO vote (disputeId, userId, vote) 
            VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE vote = ?';
        $this->_table->getAdapter()->query($sql, array(
            $data['disputeId'],
            $data['userId'],
            $data['vote'],
            $data['vote']
        ));
        
        $where = $this->query()
            ->where('disputeId = ?', $data['disputeId'])
            ->where('userId = ?', $data['userId']);
        $this->_row = $this->_table->fetchRow($where);
        
        return $this;
    }
}